package com.blacktry.controller;

import com.blacktry.entity.enums.OprLogConst;
import com.blacktry.entity.model.AdminRole;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.entity.model.Role;
import com.blacktry.entity.model.vueVO.Option;
import com.blacktry.service.RoleService;
import com.blacktry.utils.ApiResultHandler;
import com.blacktry.utils.log.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired(required = false)
    private RoleService roleService;

    @OperLog(operModul = "用户管理-添加角色",operType = OprLogConst.ADD,operDesc = "添加角色")
    @ApiOperation("添加角色")
    @PostMapping("/role")
    public ApiResult<?> addRole(@RequestBody Role role) {
        if(!role.getName().startsWith("ROLE_")){
            role.setName("ROLE_"+role.getName());
        }
        if(roleService.save(role)){
            return ApiResultHandler.buildApiResult(200,"成功");
        }
        return ApiResultHandler.buildApiResult(-1,"失败");
    }


    @OperLog(operModul = "用户管理-删除角色",operType = OprLogConst.DELETE,operDesc = "删除角色")
    @ApiOperation("删除角色")
    @DeleteMapping("/deleteRole/{id}")
    public ApiResult<Integer> deleteRole(@PathVariable("id") Integer rid) {
        if(roleService.removeById(rid)){
            return ApiResultHandler.buildApiResult(200,"成功");
        }
        return ApiResultHandler.buildApiResult(-1,"失败");
    }


    @OperLog(operModul = "用户管理-根据ids批量删除角色",operType = OprLogConst.DELETE,operDesc = "根据ids批量删除角色")
    @ApiOperation("根据ids批量删除角色")
    @DeleteMapping("/role/{ids}")
    public ApiResult<Integer> removeNoticesByIds(@PathVariable("ids") List<Integer> ids) {
        return ApiResultHandler.buildApiResult(200,"成功",roleService.removeRolesByIds(ids));
    }


    @OperLog(operModul = "用户管理-修改角色信息",operType = OprLogConst.UPDATE,operDesc = "修改角色信息")
    @ApiOperation("修改角色信息")
    @PutMapping("/role")
    public ApiResult<Integer> updateNotice(@RequestBody Role role) {
        return ApiResultHandler.buildApiResult(200,"成功",roleService.updateRole(role));
    }


    @OperLog(operModul = "用户管理-通过id获取单个角色",operType = OprLogConst.SELECT,operDesc = "通过id获取单个角色")
    @ApiOperation("通过id获取单个角色")
    @GetMapping("/role/{id}")
    public ApiResult<Role> roleById(@PathVariable("id") Integer id){
        return ApiResultHandler.buildApiResult(200,"成功",roleService.getOneById(id));
    }

    @OperLog(operModul = "用户管理-通过用户id获取该用户所有角色",operType = OprLogConst.SELECT,operDesc = "通过用户id获取该用户所有角色")
    @ApiOperation("通过用户id获取该用户所有角色")
    @GetMapping("/rolesById/{id}")
    public ApiResult<AdminRole> rolesById(@PathVariable("id") Integer id){
        return ApiResultHandler.buildApiResult(200,"成功",roleService.rolesById(id));
    }

    @OperLog(operModul = "用户管理-获取所有角色",operType = OprLogConst.SELECT,operDesc = "获取所有角色")
    @ApiOperation("获取所有角色")
    @GetMapping("/roles")
    public ApiResult<List<Role>> getAllRoles(){
        return ApiResultHandler.buildApiResult(200,"成功",roleService.list());
    }

    @OperLog(operModul = "用户管理-获取所有角色只要数组名",operType = OprLogConst.SELECT,operDesc = "获取所有角色只要数组名")
    @ApiOperation("获取所有角色只要数组名")
    @GetMapping("/getRolesStringArr")
    public String[] getRolesStringArr(){
        return roleService.getRolesStringArr();
    }

    @OperLog(operModul = "角色管理-查看角色的下拉框",operType = OprLogConst.SELECT,operDesc = "查看角色的下拉框")
    @ApiOperation("查看角色的下拉框")
    @GetMapping("/selectAllRoleOptions")
    public List<Option> selectAllRoleOptions(){
        return roleService.selectAllRoleOptions();
    }

}